<?php
class rating extends connect
{
	var $db;
	var $db_rating;
	function __construct($db)
	{
		$this->db=$db;
		$this->db_rating=$db.'r';

		echo ('
		<link rel="stylesheet" type="text/css" href="../style/rating2060.css" />
		');
	}
	function echo_rating($ask, $current, $votes)
	{
		$r=round($current, 2);
		$current=$current*20;
		echo ('
		<div class="rating">
			<table border="0" cellspacing="0" cellpadding="0">
				<tr height="18">
					<td width="105">
						<ul>
							<li class="current" title="'.$r.'');
							 if($r==5)
							 {echo " баллов";}
							 else {echo " балла";}
							 echo ('
							 " style="width: '.$current.'%">
						</ul>
					</td>
					<td width="40">
						<div class="votes" title="проголосовавших">
							('.$votes.')
						</div>
					</td>
				</tr>');
			if($ask==1)
			{
				echo ('
				<tr height="10" rowspan="2">
						<td align="center">
							<div id="voice">
							Ваш голос учтен!
							</div>
						</td>
				</tr>
				');
			}
			echo ('
			</table>
		</div>
		');
	}
	function show_rating($city, $id)
	{
		parent::__construct($this->db);
		$query="SELECT rating, votes FROM $city WHERE id='$id'";
		$res=mysql_query($query) or die(mysql_error());
		$row=mysql_fetch_array($res);
		$current=$row['rating'];
		$this->echo_rating(0, $current, $row['votes']);
	}
	function rating($city, $id)
	{
		parent::__construct($this->db_rating);
		$rate=$_GET['rate'];
		$date=date("Y-m-d H:i:s");
		$user_ip=$_SERVER['REMOTE_ADDR'];
		$query="SELECT firm_id, rating, votes, user FROM $city
			WHERE firm_id='$id' && user='$user_ip'"; //голосовал
		$res=mysql_query($query) or die(mysql_error());
		$row=mysql_fetch_array($res);
        $current=$row['rating'];
        if(isset($row['rating']))
        {
			$this->echo_rating(1, $current, $row['votes']);
		}
		else if(isset($_COOKIE[$id]))
		{
			$query="SELECT firm_id, rating, votes, data FROM $city
				WHERE firm_id='$id' ORDER BY data DESC LIMIT 1";
			$res=mysql_query($query) or die(mysql_error());
			$row=mysql_fetch_array($res);
			$current=$row['rating'];
			$this->echo_rating(1, $current, $row['votes']);
		}
		else                    //если не голосовал
		{
			$query="SELECT id, firm_id, rating, votes, user, data FROM $city
			WHERE firm_id='$id' ORDER BY data DESC LIMIT 1";
			$res=mysql_query($query) or die(mysql_error());
			$row=mysql_fetch_array($res);

			if(isset($_GET['rate']) && $_GET['rate']!="" && $_GET['rate']!=0) //если полбзователь голосует
				{
					if($row['votes']<1)
          					{
							$rating=0;
							$votes=0;  //ecли никто до этого не голосовал присваиваем нули рейтингу и голосам
          					}

					else
         					{
							$rating=$row['rating'];  //иначе присваиваем последние значения
							$votes=$row['votes'];
							}

					$rate=$_GET['rate'];

					$new_votes=$votes+1;
					$new_rate=(($rating*$votes)+$rate)/($votes+1);

					$query="INSERT INTO $city SET  firm_id='".$id."', user='".$user_ip."',
					rating='".$new_rate."', votes='".$new_votes."', data='$date'";
					mysql_query($query) or die(mysql_error());

					//$query="SELECT id, firm_id, rating, votes, user, data FROM $city
					//WHERE firm_id='$id' && user='$user_ip'";
					//$res=mysql_query($query) or die(mysql_error());
					//$row=mysql_fetch_array($res);
					$current=$new_rate;//получаем результат в процентах
					$this->echo_rating(1, $current, $new_votes);
					parent::__construct($this->db);
					$query="UPDATE $city SET
					rating='".$new_rate."',
					votes='".$new_votes."'
					WHERE id='".$id."'";
					mysql_query($query) or die(mysql_error());
					mysql_close();
				}
			else
				{
					$current=$row['rating']*20;
				echo ('
		<div class="rating">
			<table border="0">
				<tr height="20">
					<td width="105">
						<ul>
							<li class="current" style="width: '.$current.'%">
							<a rel="nofollow" href="'.$_SERVER['REQUEST_URI'].'" class="star1" title="Рейтинг 1 из 5"></a></li>
							<li><a rel="nofollow" href="'.$_SERVER['REQUEST_URI'].'&rate=2" class="star2" title="Рейтинг 2 из 5"></a></li>
							<li><a rel="nofollow" href="'.$_SERVER['REQUEST_URI'].'&rate=3" class="star3" title="Рейтинг 3 из 5"></a></li>
							<li><a rel="nofollow" href="'.$_SERVER['REQUEST_URI'].'&rate=4" class="star4" title="Рейтинг 4 из 5"></a></li>
							<li><a rel="nofollow" href="'.$_SERVER['REQUEST_URI'].'&rate=5" class="star5" title="Рейтинг 5 из 5"></a></li>
						</ul>
					</td>
					<td width="40">
						<div class="votes" title="проголосовавших">
							('.$row['votes'].')
						</div>
					</td>
			    </tr>
			</table>
        </div>
			');
				}
		}

	}
}
?>